package com.liangzai.proxy.service;

import com.liangzai.proxy.service.cglib.CglibMethodInterceptor;
import com.liangzai.proxy.service.impl.OrderServiceImpl;
import com.liangzai.proxy.service.jdk.JDKOrderService;
import com.liangzai.proxy.service.jdk.JDKOrderServiceImpl;
import com.liangzai.proxy.service.jdk.JdkInvocationHandler;
import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.cglib.proxy.Enhancer;

/**
 * @author leiliang
 * @version 1.0
 * @Description
 * @date 2021/3/15 18:03
 */
public class TestClient {
    public static void main(String[] args) {
        //静态代理  实现接口的方式
        OrderServiceProxy orderServiceProxy = new OrderServiceProxy(new OrderServiceImpl());
        orderServiceProxy.order();

        //静态代理继承的形式
        OrderService orderService = new OrderServiceProxyExtends();
        orderService.order();


        //jdk的动态代理
        JdkInvocationHandler jdkInvocationHandler = new JdkInvocationHandler(new JDKOrderServiceImpl());
        JDKOrderService proxy = jdkInvocationHandler.getProxy();
        proxy.order();
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");


        //cglib
        System.out.println("测试cglib");
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\code");
        CglibMethodInterceptor cglibMethodInterceptor = new CglibMethodInterceptor();
        Enhancer enhancer = new Enhancer();
    // 设置代理类的付类
        enhancer.setSuperclass(OrderServiceImpl.class);
    // 设置回调对象
        enhancer.setCallback(cglibMethodInterceptor);
    // 创建代理对象
        OrderServiceImpl orderServiceImpl = (OrderServiceImpl) enhancer.create();
        orderServiceImpl.order();
    }
}
